//
// Created by xili on 2024/9/3 20:03.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int minDeletions(string s) {
        int cnt[26] = {0};
        for (auto &it: s) {
            cnt[it - 'a']++;
        }
        int ans = 0;
        unordered_set<int> us;
        for (int i = 0; i < 26; i++) {
            if (cnt[i] == 0) {
                continue;
            }
            if (us.count(cnt[i])) {
                int j = cnt[i];
                while (cnt[i] > 0 && us.count(cnt[i])) {
                    cnt[i]--;
                }
                if (cnt[i]) {
                    us.insert(cnt[i]);
                }
                ans += j - cnt[i];
            } else {
                us.insert(cnt[i]);
            }
        }
        return ans;
    }
};